package com.atguigu;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: Test01
 * Package: com.atguigu
 * Description:
 *
 * @Author ChenJun
 * @Create 2023/4/22 20:30
 * @Version 1.0
 */
public class Test01 {
    // TODO MAIN 方法
    public static void main(String[] args) throws IOException {
        //TODO 插入数据
//        putCell("bigdata","student","1001","info","name","zhangsan");

        //TODO 查询方法
//        String cell = getCell("bigdata", "student", "1001", "info", "name");
//        System.out.println(cell);

        //TODO 扫描数据
//        putCell("bigdata","student","1002","info","name","lisi");
//        String cell = getCell("bigdata", "student", "1001", "info", "name");
//        System.out.println(cell);
//
//        List<String> strings = scanRows("bigdata", "student", "1001", "2000");
//
//        for (String string : strings) {
//            System.out.println(string);
//        }

        //TODO 删除数据
        putCell("bigdata","student","1002","info","name","lisi");
//        String cell = getCell("bigdata", "student", "1001", "info", "name");
//        System.out.println(cell);
//        List<String> strings = scanRows("bigdata", "student", "1001", "2000");
//        for (String string : strings) {
//            System.out.println(string);
        deleteColumn("bigdata", "student", "1001", "info", "name");

        //关闭连接
        connection.close();
    }
    private static Connection connection ;
    // 设置静态属性hbase连接
    static {
        Configuration configuration = HBaseConfiguration.create();
        try {
            connection = ConnectionFactory.createConnection(configuration);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }



    //TODO 插入数据
    /**
     * putCell数据
     * @param nameSpace
     * @param tableName
     * @param rowKey
     * @param family
     * @param column
     * @param value
     * @throws IOException
     */
    public static void putCell(String nameSpace,String tableName,String rowKey,String family,String column,String value) throws IOException {
        // 1.获取table
        Table table = connection.getTable(TableName.valueOf(nameSpace, tableName));

        // 2.创建Put对象
        Put put = new Put(Bytes.toBytes(rowKey));

        // 3.添加put属性
        put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));

        // 3.put数据
        table.put(put);

        // 5.关闭资源
        table.close();
    }

    // TODO 查询方法
    /**
     * getCell数据
     * @param nameSpace
     * @param tableName
     * @param rowKey
     * @param family
     * @param column
     * @return
     * @throws IOException
     */
    public static String getCell(String nameSpace,String tableName,String rowKey,String family,String column) throws IOException {

        // 1.获取table
        Table table = connection.getTable(TableName.valueOf(nameSpace, tableName));

        // 2.获取Get对象
        Get get = new Get(Bytes.toBytes(rowKey));

        // 3.添加get属性
        get.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));

        // 3.get数据
        // 简便用法
//        byte[] bytes = table.get(get).value();
//        String value = new String(bytes);
        // 复杂用法
        // 3.1 获取result
        Result result = table.get(get);
        // 3.2 获取cells
        Cell[] cells = result.rawCells();
        // 3.3 遍历cells
        String value = "";
        for (Cell cell : cells) {
            // 3.4 输出每个cell
            value += Bytes.toString(CellUtil.cloneValue(cell)) + "-";
        }

        // 5.关闭资源
        table.close();

        return value;
    }

    //TODO 扫描数据
    /**
     * 扫描数据
     * @param nameSpace
     * @param tableName
     * @param startRow
     * @param stopRow
     * @return
     * @throws IOException
     */
    public static List<String> scanRows(String nameSpace, String tableName, String startRow, String stopRow) throws IOException {

        // 1.获取table
        Table table = connection.getTable(TableName.valueOf(nameSpace, tableName));

        // 2.创建Scan对象
        Scan scan = new Scan().withStartRow(Bytes.toBytes(startRow)).withStopRow(Bytes.toBytes(stopRow));

        // 3.扫描数据
        ResultScanner scanner = table.getScanner(scan);

        // 3.获取结果
        ArrayList<String> arrayList = new ArrayList<>();
        for (Result result : scanner) {
            arrayList.add(Bytes.toString(result.value()));
        }

        // 5.关闭资源
        scanner.close();
        table.close();

        return arrayList;
    }


    //TODO 删除数据
    /**
     * 删除column数据
     *
     * @param nameSpace
     * @param tableName
     * @param rowKey
     * @param family
     * @param column
     * @throws IOException
     */
    public static void deleteColumn(String nameSpace, String tableName, String rowKey, String family, String column) throws IOException {

        // 1.获取table
        Table table = connection.getTable(TableName.valueOf(nameSpace, tableName));

        // 2.创建Delete对象
        Delete delete = new Delete(Bytes.toBytes(rowKey));

        // 3.添加删除信息
        // 3.1 删除单个版本
//        delete.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));
        // 3.2 删除所有版本
        delete.addColumns(Bytes.toBytes(family), Bytes.toBytes(column));
        // 3.3 删除列族
//        delete.addFamily(Bytes.toBytes(family));

        // 3.删除数据
        table.delete(delete);

        // 5.关闭资源
        table.close();
    }
}
